package com.netsuite.helper;

import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.TransferHandler;

public class TransferHelper extends TransferHandler {  

	private static final long serialVersionUID = 1L;

	public TransferHelper() {  
    }  
  
    @Override  
    public int getSourceActions(JComponent c) {  
        return MOVE;  
    }  
  
    @Override  
    protected Transferable createTransferable(JComponent source) {  
    	String data = (String) ((JTable) source).getModel().getValueAt(((JTable) source).getSelectedRow(), ((JTable) source).getSelectedColumn());
        data = (String) ((JTable) source).getModel().getValueAt(((JTable) source).getSelectedRow(), ((JTable) source).getSelectedColumn())+":"+String.valueOf(((JTable) source).getSelectedRow())+":"+String.valueOf(((JTable) source).getSelectedColumn());
    	return new StringSelection(data);  
    }  
  
    @Override  
    protected void exportDone(JComponent source, Transferable data, int action) {  
    }  
  
    @Override  
    public boolean canImport(TransferSupport support) {  
    	boolean canImport = false;
    	JTable target = (JTable)support.getComponent();
        DropLocation dl = support.getDropLocation();
        Point dp = dl.getDropPoint();
        int dragColumn = target.columnAtPoint(dp);
        
        try {
        	Transferable t = support.getTransferable();
			String cd = (String) t.getTransferData(DataFlavor.stringFlavor);
			String [] splitString = cd.split(":");
			 if (dragColumn == Integer.parseInt(splitString[2])) {
				 canImport = true;
			 }
		} catch (UnsupportedFlavorException e) {
			MessageViewHelper.displayErrorView(e);
		} catch (IOException e) {
			MessageViewHelper.displayErrorView(e);
		}
        
        return canImport;  
    }  
  
    @Override  
    public boolean importData(TransferSupport support) {
    	boolean imported = false;
        JTable target = (JTable) support.getComponent();  
        
        DropLocation dl = support.getDropLocation();
        Point dp = dl.getDropPoint();
        int dropCol = target.columnAtPoint(dp);
        int dropRow = target.rowAtPoint(dp);
        String dropValue = (String) target.getValueAt(dropRow, dropCol);
        try {
            Transferable t = support.getTransferable();
            String cd = (String) t.getTransferData(DataFlavor.stringFlavor);
			String [] splitString = cd.split(":");
			target.setValueAt(dropValue, Integer.parseInt(splitString[1]), Integer.parseInt(splitString[2]));
			target.setValueAt(splitString[0], dropRow, dropCol);
            imported = true;
        } catch (UnsupportedFlavorException | IOException ex) {
            ex.printStackTrace();
        } 
        return imported;  
    }  
}  
